package com.xuexi.export;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/*public class DataExporter {

    private static final String DB_URL = "jdbc:mysql://localhost:3306/your_database";
    private static final String USER = "your_user";
    private static final String PASSWORD = "your_password";
    private static final String OUTPUT_FILE = "exported_data.csv";

    // 导出数据的方法
    public static void exportData(int startId, int endId) {
        String query = "SELECT * FROM users WHERE status = 'active' AND id >= ? AND id < ?";

        try (Connection conn = DriverManager.getConnection(DB_URL, USER, PASSWORD);
             PreparedStatement pstmt = conn.prepareStatement(query);
             BufferedWriter writer = new BufferedWriter(new FileWriter(OUTPUT_FILE, true))) {

            pstmt.setInt(1, startId);
            pstmt.setInt(2, endId);
            ResultSet rs = pstmt.executeQuery();

            while (rs.next()) {
                // 假设我们只想导出 id 和 name 字段，实际情况可以根据需要更改
                int id = rs.getInt("id");
                String name = rs.getString("name");
                writer.write(id + "," + name);
                writer.newLine();
            }
        } catch (SQLException | IOException e) {
            e.printStackTrace();
        }
    }

    // 生成数据分区
    public static List<int[]> generatePartitions(int totalRecords, int partitionSize) {
        List<int[]> partitions = new ArrayList<>();
        for (int i = 1; i <= totalRecords; i += partitionSize) {
            int endId = Math.min(i + partitionSize, totalRecords + 1);
            partitions.add(new int[]{i, endId});
        }
        return partitions;
    }

    public static void main(String[] args) {
        int totalRecords = 10000000; // 假设总记录数
        int partitionSize = 100000;   // 每个线程处理的记录数
        int numThreads = 5;           // 线程数量

        List<int[]> partitions = generatePartitions(totalRecords, partitionSize);

        // 清空输出文件
        try {
            new BufferedWriter(new FileWriter(OUTPUT_FILE)).close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        ExecutorService executor = Executors.newFixedThreadPool(numThreads);
        for (int[] partition : partitions) {
            executor.submit(() -> exportData(partition[0], partition[1]));
        }

        executor.shutdown();
        while (!executor.isTerminated()) {
            // 等待所有任务完成
        }

        System.out.println("Data export completed.");
    }
}*/

public class DataExporter {

    public static void main(String[] args) throws SQLException, IOException {

        String OUTPUT_FILE = "exported_data.csv";
        Connection connection = ConnectionUtil.connection;
        System.out.println("1:" + connection.isClosed());

        String sql = "SELECT event_name from workorderlist WHERE workorder = ? and event_name = ? ";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setString(1, "");
        preparedStatement.setString(2, "");

        ResultSet rs = preparedStatement.executeQuery();
        BufferedWriter writer = new BufferedWriter(new FileWriter(OUTPUT_FILE, true));
        while (rs.next()) {
            // 假设我们只想导出 id 和 name 字段，实际情况可以根据需要更改
            int id = rs.getInt("id");
            String name = rs.getString("name");
            writer.write(id + "," + name);
            writer.newLine();
        }

        ConnectionUtil.close();
        System.out.println("2:" + connection.isClosed());
    }
}
